using System;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;

namespace EntityKit.Runtime
{
    /// <summary>
    /// 默认平台存储适配器
    /// </summary>
    public class DefaultPlatformStorageAdapter : IPlatformStorageAdapter
    {
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="data">文件数据</param>
        public void WriteFile(string path, string data)
        {
            try
            {
                File.WriteAllText(path, data);
            }
            catch (Exception ex)
            {
                Debug.LogError($"写入文件失败: {ex.Message}");
                throw new StorageException("写入文件失败", ex);
            }
        }
        
        /// <summary>
        /// 异步写入文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="data">文件数据</param>
        public async Task WriteFileAsync(string path, string data)
        {
            try
            {
                // 使用Task.Run确保IO操作在线程池线程上执行，避免阻塞主线程
                await Task.Run(() => File.WriteAllText(path, data));
            }
            catch (Exception ex)
            {
                Debug.LogError($"异步写入文件失败: {ex.Message}");
                throw new StorageException("异步写入文件失败", ex);
            }
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>文件数据</returns>
        public string ReadFile(string path)
        {
            try
            {
                return File.ReadAllText(path);
            }
            catch (Exception ex)
            {
                Debug.LogError($"读取文件失败: {ex.Message}");
                throw new StorageException("读取文件失败", ex);
            }
        }
        
        /// <summary>
        /// 异步读取文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>文件数据</returns>
        public async Task<string> ReadFileAsync(string path)
        {
            try
            {
                // 使用Task.Run确保IO操作在线程池线程上执行，避免阻塞主线程
                return await Task.Run(() => File.ReadAllText(path));
            }
            catch (Exception ex)
            {
                Debug.LogError($"异步读取文件失败: {ex.Message}");
                throw new StorageException("异步读取文件失败", ex);
            }
        }

        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>是否存在</returns>
        public bool FileExists(string path) => File.Exists(path);

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path">文件路径</param>
        public void DeleteFile(string path)
        {
            try
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
            }
            catch (Exception ex)
            {
                Debug.LogError($"删除文件失败: {ex.Message}");
                throw new StorageException("删除文件失败", ex);
            }
        }
        
        /// <summary>
        /// 异步删除文件
        /// </summary>
        /// <param name="path">文件路径</param>
        public async Task DeleteFileAsync(string path)
        {
            try
            {
                // 使用Task.Run确保IO操作在线程池线程上执行，避免阻塞主线程
                await Task.Run(() =>
                {
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                });
            }
            catch (Exception ex)
            {
                Debug.LogError($"异步删除文件失败: {ex.Message}");
                throw new StorageException("异步删除文件失败", ex);
            }
        }
    }
}